package com.apts.controller;

import java.util.HashMap;
import java.util.Map;

import com.apts.jfinalbase.BaseController;
import com.apts.jfinalbase.BaseJsonPojo;
import com.apts.pojo.ErrorPojo;
import com.apts.servise.IndexAdminServise;
import com.apts.util.MD5Util;
import com.apts.validator.DoLoginValidator;
import com.jfinal.aop.Before;
import com.jfinal.upload.UploadFile;

/**
 * 后台欢迎页面Controller
 * 
 * @author lwh
 * @date 2016-03-20
 */
public class IndexAdminController extends BaseController {

	public void index() {
		render("/index.html"); // 跳到首页页面
	}
	
	/**
	 * 后台登陆
	 */
	@Before(DoLoginValidator.class)
	public void doLogin() {
		System.out.println("session: -------------------------- " + this.getSession().getId());
		String code = this.getPara("code");
		if (code != null && code.equals("666")) {
			BaseJsonPojo json = IndexAdminServise.Servise.doLogin(this.getPara("account"), this.getPara("password"));
			this.renderJson(json);
		} else {
			BaseJsonPojo json = new ErrorPojo("验证码错误");
			this.renderJson(json);
//			this.renderFile();
		}
	}

	// 管理员注册
	public void Register() {
		System.out.println("session: -------------------------- " + this.getSession().getId());

		UploadFile upload = getFile("picture");// 上传
		String picture = upload.getUploadPath();
		String account = this.getPara("account");
		String password = this.getPara("password");
		String pwdsalt = "salt";
		String email = this.getPara("email");
		String telephone = this.getPara("telephone");
		password = MD5Util.md5Encode(password + "salt");

		System.out.println("图片上传的路径是" + picture);
		System.out.println("注册的方法里面密码是" + password);

		Map<String, Object> map = new HashMap<String, Object>(0);

		map.put("account", account);
		map.put("password", password);
		map.put("pwdsalt", pwdsalt);
		map.put("email", email);
		map.put("telephone", telephone);
		map.put("picture", picture);

		IndexAdminServise.Servise.register(map);
	}

}
